package shaka.ui

import groovy.json.JsonBuilder
import groovy.json.JsonSlurper

import java.util.regex.Matcher
import java.util.regex.Pattern

import org.eclipse.swt.SWT
import org.eclipse.swt.custom.CTabFolder
import org.eclipse.swt.widgets.Text

import shaka.groovy.swt.SwtBuilder

class SwtRegPaserPanel {
	private static FUNC = 'SwtBeanPaserPanel'
	static panel(SwtBuilder swt,String fontType,int fontSize,fontStyle){
		Map w = [:]
		Closure saveCB = { String key ->
			Text text_src = w['text_src']
			Text text_sch = w['text_sch']
			Text text_rep = w['text_rep']

			Map saveArgs = [
				text_src:text_src.text,
				text_sch:text_sch.text,
				text_rep:text_rep.text
			]

			JsonBuilder jb = new JsonBuilder()
			jb(saveArgs)
			FuncStorage.instance.func(FUNC, key, jb.toString())
		}
		swt.cTabItem(c:[SWT.NONE], text:'正規表示式'){
			font(c:[fontType, fontSize, fontStyle])
			composite(c:[SWT.BORDER]){
				formLayout()
				w['1-exec_btn'] = button(c:[SWT.PUSH], text:'執行'){
					formData(top:[0, 0], right:[100, -3])
					// 執行 - 程式碼產生
					event(click:{
						Text text_src = w['text_src']
						Text text_sch = w['text_sch']
						Text text_rep = w['text_rep']

						Text text_rst = w['text_rst']

						if(!text_src.text)
							return
						if(!text_sch.text)
							return
						if(!text_rep.text)
							return
						Matcher m = Pattern.compile(text_sch.text).matcher(text_src.text)
						StringBuffer s = new StringBuffer()
						while(m.find()){
							String match = m.group()
							m.appendReplacement(s, text_rep.text)
						}
						m.appendTail(s)

						text_rst.text = s.toString()

						CTabFolder cTabFolder = w['tab']
						cTabFolder.setSelection(1)
					})
				}
				label(c:[SWT.SEPARATOR | SWT.VIRTUAL]){
					formData(top:[0, 5], right:[w['1-exec_btn'], -3], bottom:[0, 20])
				}
				w['1-load_btn'] = button(c:[SWT.PUSH], text:'取出'){
					formData(top:[0, 0], right:[w['1-exec_btn'], -10])
					event(click:{
						SwtApplication.dialogGet(FUNC, { String value ->
							// get
							JsonSlurper js = new JsonSlurper()
							Map args = js.parseText(value)

							Text text_src = w['text_src']
							Text text_sch = w['text_sch']
							Text text_rep = w['text_rep']

							text_src.text = args.text_src
							text_sch.text = args.text_sch
							text_rep.text = args.text_rep
						}, { String key ->
							// save
							saveCB(key)
						}, { String key, String text->
							// rename
							FuncStorage.instance.rename(FUNC, key, text)
						}, { String key ->
							// delete
							FuncStorage.instance.delete(FUNC, key)
						})
					})
				}
				w['1-save_btn'] = button(c:[SWT.PUSH], text:'儲存'){
					formData(top:[0, 0], right:[w['1-load_btn'], 0])
					event(click:{ SwtApplication.dialogSave(saveCB) })
				}
				w['tab'] = cTabFolder(c:[SWT.TOP]){
					formData(top:[0, 5], left:[0, 0], right:[100, 0], bottom:[100, 0])
					cTabItem(c:[SWT.NONE], text:'參數設置'){
						font(c:[fontType, fontSize, fontStyle])
						composite(c:[SWT.BORDER]){
							formLayout()
							group(c:[SWT.NONE], text:'來源'){
								formData(top:[0, 0], left:[0, 0], right:[100, 0], bottom:[40, 0])
								fillLayout()
								w['text_src'] = text(c:[SWT.MULTI | SWT.BORDER | SWT.V_SCROLL]){
									font(c:[fontType, fontSize, fontStyle])
									ctrla()
								}
							}
							group(c:[SWT.NONE], text:'搜尋'){
								formData(top:[40, 0], left:[0, 0], right:[100, 0], bottom:[70, 0])
								fillLayout()
								w['text_sch'] = text(c:[SWT.MULTI | SWT.BORDER | SWT.V_SCROLL]){
									font(c:[fontType, fontSize, fontStyle])
									ctrla()
								}
							}
							group(c:[SWT.NONE], text:'置換'){
								formData(top:[70, 0], left:[0, 0], right:[100, 0], bottom:[100, 0])
								fillLayout()
								w['text_rep'] = text(c:[SWT.MULTI | SWT.BORDER | SWT.V_SCROLL]){
									font(c:[fontType, fontSize, fontStyle])
									ctrla()
								}
							}
						}
					}
					cTabItem(c:[SWT.NONE], text:'產生結果'){
						font(c:[fontType, fontSize, fontStyle])
						composite(c:[SWT.BORDER]){
							fillLayout()
							w['text_rst'] = text(c:[SWT.MULTI | SWT.BORDER | SWT.V_SCROLL]){
								font(c:[fontType, fontSize, fontStyle])
								ctrla()
							}
						}
					}
					selection(0)
				}
			}
		}
	}
}
